//
//  DHPopPathButton.m
//  BaseProject
//
//  Created by dadahua on 17/5/14.
//  Copyright © 2017年 dahua. All rights reserved.
//

#import "DHPopPathButton.h"

// 半径
#define kDHPopPathButtonRadius SCREEN_W_X(4)
#define MP(x) M_PI * x / 180.f
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
#define SCREEN_W_X(X) ([UIScreen mainScreen].bounds.size.width / X)
#define SCREEN_H_X(X) ([UIScreen mainScreen].bounds.size.height / X)


@interface DHPopPathButton ()

@property (nonatomic, assign) CGPoint orginalPoint;
@property (nonatomic, strong) NSMutableArray<DHPopPathItem *> *items;
@property (nonatomic, assign) kDHPathButtonDirection direction;
@property (nonatomic, assign) id<DHPopPathButtonDelegate> delegate;

@end

@implementation DHPopPathButton

+ (instancetype)popWithClickPoint:(CGPoint)centePoint
                            items:(NSMutableArray<DHPopPathItem *> *)items
                         delegate:(id<DHPopPathButtonDelegate>)delegate{
    
    return [[self alloc] initWithFrame:[UIScreen mainScreen].bounds
                            ClickPoint:centePoint
                                 items:items
                              delegate:delegate];
}

- (instancetype)initWithFrame:(CGRect)frame
                   ClickPoint:(CGPoint)centePoint
                        items:(NSMutableArray<DHPopPathItem *> *)items
                     delegate:(id<DHPopPathButtonDelegate>)delegate{
    
    if (self = [super initWithFrame:frame]) {
        self.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        self.items = items;
        self.orginalPoint = centePoint;
        self.delegate = delegate;
        [self addGestureRecognizer:({
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureReture)];
            tap;
        })];
        [self configDirection];
        [self addContentView];
    }
    return self;
}



#pragma mark - UI
- (void)addContentView {
    
    [self addSubview:({
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.center = self.orginalPoint;
        btn.bounds = CGRectMake(0, 0, 40, 40);
        btn.layer.cornerRadius = 20;
        [btn setImage:[UIImage imageNamed:@"返回-1"] forState:UIControlStateNormal];
//        [btn setTitle:@"返回" forState:UIControlStateNormal];
        
        [btn addSubview:({
            UILabel *lab = [UILabel new];
            lab.bounds = CGRectMake(0, 0, btn.bounds.size.width, 10);
            lab.center = CGPointMake(btn.bounds.size.width/2, btn.bounds.size.height - 5);
            lab.text = @"返回";
            lab.textColor = [UIColor whiteColor];
            lab.textAlignment = NSTextAlignmentCenter;
            lab.font = [UIFont systemFontOfSize:8.f];
            lab;
        })];
        
        
        [btn addTarget:self action:@selector(centerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setBackgroundColor:[UIColor lightGrayColor]];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
        btn;
    })];
    
    for (DHPopPathItem *item in self.items) {
        [self addSubview:item];
        item.center = self.orginalPoint;
        [item addTarget:self
                 action:@selector(itemDidClick:)
       forControlEvents:UIControlEventTouchUpInside];
    }
    
}

//- (void)willMoveToSuperview:(UIView *)newSuperview {
//    
//}

- (void)didMoveToSuperview {
    
    [super didMoveToSuperview];
    CGFloat orginalX = self.orginalPoint.x;
    CGFloat orginalY = self.orginalPoint.y;
    NSInteger count = self.items.count;
    [UIView animateWithDuration:0.3
                          delay:0
         usingSpringWithDamping:0.5
          initialSpringVelocity:12
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         switch (self.direction) {
                             case kDHPathButtonDirectionTop:
                             case kDHPathButtonDirectionRight:
                             case kDHPathButtonDirectionBottom:
                             case kDHPathButtonDirectionLeft:
                             {
                                 double orginalAngle =270 - 90 * (int)self.direction;
                                 for (int i = 0; i < count; i ++) {
                                     DHPopPathItem *item = self.items[i];
                                     item.bounds = CGRectMake(0, 0, DHPopPathItemWidth, DHPopPathItemHeight);
                                     double angle = orginalAngle - (180 / (double)(count - 1)) * i;
                                     item.center = CGPointMake(orginalX + kDHPopPathButtonRadius * sin(MP(angle)), orginalY + kDHPopPathButtonRadius * cos(MP(angle)));
                                 }
                                 
                             }
                                 break;
                                 
                             default:
                             {
                                 double orginalAngle = 90 - 90 * (int)(self.direction - 4);
                                 for (int i = 0; i < count; i ++) {
                                     DHPopPathItem *item = self.items[i];
                                     item.bounds = CGRectMake(0, 0, DHPopPathItemWidth, DHPopPathItemHeight);
                                     double angle = orginalAngle - (90 / (double)(count - 1)) * i;
                                     NSLog(@"%ld度数是多少呢-->%lf,-->%lf",self.direction,angle,sin(MP(angle)));
                                     item.center = CGPointMake(orginalX + kDHPopPathButtonRadius * cos(MP(angle)), orginalY - kDHPopPathButtonRadius * sin(MP(angle)));
                                 }
                                 
                             }
                                 break;
                         }

                     }
                     completion:^(BOOL finished) {
                         
                     }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
//    [self removeFromSuperview];
}



#pragma mark - action
- (void)gestureReture {
    [self removeFromSuperview];
//    return;
}
- (void)centerBtnClick:(UIButton *)sender {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(DHPopPathButtonDidSelectCenterBtn)]) {
        [self.delegate DHPopPathButtonDidSelectCenterBtn];
    }
    [self removeFromSuperview];
}

- (void)itemDidClick:(DHPopPathItem *)sender {
    
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(DHPopPathButton:didSelectIndex:)]) {
        [self.delegate DHPopPathButton:self didSelectIndex:[self.items indexOfObject:sender]];
    }
    [self removeFromSuperview];
}



#pragma mark - private
// 找到方向
- (void)configDirection {
    
    CGFloat orginalX = self.orginalPoint.x;
    CGFloat orginalY = self.orginalPoint.y;

    // 先通过x分类
    if (orginalX <= SCREEN_W_X(4)) {
        
        if (orginalY <= SCREEN_W_X(4)) {
            self.direction = kDHPathButtonDirectionTopLeft ;
        } else if (orginalY >= SCREEN_H - SCREEN_W_X(4)) {
            self.direction = kDHPathButtonDirectionBottomLeft;
        } else{
            self.direction = kDHPathButtonDirectionLeft;
        }
        
    } else if (orginalX >= SCREEN_W_X(4) * 3) {
        
        if (orginalY <= SCREEN_W_X(4)) {
            self.direction = kDHPathButtonDirectionTopRight  ;
        } else if (orginalY >= SCREEN_H - SCREEN_W_X(4)) {
            self.direction = kDHPathButtonDirectionBottomRight ;
        } else{
            self.direction = kDHPathButtonDirectionRight;
        }
        
    } else{
        
        
        if (orginalY <= SCREEN_W_X(4)) {
            self.direction = kDHPathButtonDirectionTop;
        } else {
            self.direction = kDHPathButtonDirectionBottom;
        }
    }
    
}


@end
